package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FinanceDao;
import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.domain.cargo.Finance;
import cn.itcast.domain.cargo.FinanceExample;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.InvoiceExample;
import cn.itcast.service.cargo.FinanceSrevice;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service(timeout = 200000) // 设置超时时间为200秒
public class FinanceSreviceImpl implements FinanceSrevice {
    @Autowired
    private FinanceDao financeDao;
    @Autowired
    private InvoiceDao invoiceDao;

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<Finance> findByPage(FinanceExample financeExample, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Finance> list = financeDao.selectByExample(financeExample);
        return new PageInfo<>(list);
    }
    // 货物添加

    @Override
    public void save(Finance finance) {
        //1. 设置主键
        finance.setId(UUID.randomUUID().toString());
        //2. 初始化数据
//        String invoiceNo = finance.getInvoiceNo();

        finance.setState(0);//不动
        finance.setInputDate(new Date());

        String invoiceId = finance.getInvoiceId();
        InvoiceExample example = new InvoiceExample();
        InvoiceExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(invoiceId);

        List<Invoice> invoiceList = invoiceDao.selectByExample(example);
        if (invoiceList != null && invoiceList.size() > 0){
            for (Invoice invoice : invoiceList) {
                invoice.setStatus(1);
                invoiceDao.updateByPrimaryKeySelective(invoice);
            }
        }

        //3. 添加
        financeDao.insertSelective(finance);
    }

    @Override
    public Finance findById(String id) {
        return financeDao.selectByPrimaryKey(id);
    }
}
